package androidtool.checkconvention;

/**
 *
 * @author Hoang
 */
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLReader {

    public static final int OTHER = 0;
    public static final int JAVA = 0;
    public static final int XML = 0;
    private int typeFile;
    private List<Widget> listWidget;

    public XMLReader(String dirFile) {
        String extension = dirFile.substring(dirFile.length() - 4, dirFile.length());

        if (extension.equalsIgnoreCase("java")) {
            typeFile = JAVA;

            JOptionPane.showMessageDialog(null, ".java file is comming soon.");
        } else if (extension.equalsIgnoreCase(".xml")) {
            typeFile = XML;

            Element rootElement = getRootElement(dirFile);
            WidgetTreeNode widgetTreeNode = new WidgetTreeNode(rootElement);
            listWidget = widgetTreeNode.getAllWidgetHaveId();

        } else {
            typeFile = OTHER;
            JOptionPane.showMessageDialog(null, "Don't support this file.");
        }
    }

    public int getTypeFile() {
        return typeFile;
    }

    public List<Widget> getListWidget() {
        return listWidget;
    }

    private Element getRootElement(String dirFile) {
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(dirFile);
            return document.getRootElement();
        } catch (DocumentException ex) {
            Logger.getLogger(XMLReader.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
